package com.bac.rentmap.web.controller.autocomplete;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bac.rentmap.autocomplete.AddressAutocomleter;

/**
 * Autocompletes user input in address entry on flat ad search page.
 *
 * @author Andrew Osipenko
 */
public class AddressAutocompleteController extends AbstractController {
    private AddressAutocomleter addressAutocompleter;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String userInput = request.getParameter("q");
        List<String> proposals = addressAutocompleter.autocomplete(request.getParameter("city"),
                userInput);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("buildings", proposals);
        return new ModelAndView("autocomplete/address", model);

    }

    @Required
    public void setAddressAutocompleter(AddressAutocomleter addressAutocompleter) {
        this.addressAutocompleter = addressAutocompleter;
    }
}
